package com.pan.admin.module.channel.entity.vo;

import com.pan.admin.common.constant.module.StatisticEnum;
import com.pan.admin.common.utils.StatisticUtils;
import com.pan.admin.module.channel.entity.LoanChannelFormRateStatistic;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 渠道统计表单资质VO实体
 *
 * @author Pan
 */
@Data
public class LoanChannelFormRateStatisticVO {

    /**
     * 渠道名称
     */
    private String name;
    /**
     * 字段名称
     */
    private String fieldName;
    /**
     * 字段值名称
     */
    private List<String> fieldValueNames;
    /**
     * 字段总数
     */
    private Integer fieldTotal;
    /**
     * 创建时间
     */
    private List<String> createTime;

    private Long total;

    public static List<LoanChannelFormRateStatisticVO> fill(List<LoanChannelFormRateStatistic> list, StatisticEnum statisticEnum) {
        List<String> fillTime = StatisticUtils.getFillTime(statisticEnum);
        // 创建一个空的 Map，用于存储 fieldName 和对应的出现次数
        Map<String, LoanChannelFormRateStatisticVO> map = new HashMap<>();
        Map<String, Integer> fieldNameCountMap = new HashMap<>();
        // 遍历 LoanChannelFormRateStatistic 列表
        for (LoanChannelFormRateStatistic statistic : list) {
            String createTime = statistic.getCreateTime();
            for (int i = 0; i < fillTime.size(); i++) {
                String time = fillTime.get(i);
                // 如果 createTime 在 fillTime 列表中，则进行统计
                if (time.equals(createTime)) {
                    fieldNameCountMap = list.stream().collect(Collectors.groupingBy(t -> t.getFieldName(), Collectors.summingInt(t -> Integer.parseInt(t.getTotal()))));
                }

            }
        }
        // 创建一个空的 LoanChannelFormRateStatisticVO 列表，用于存储处理后的结果
        List<LoanChannelFormRateStatisticVO> result = new ArrayList<>();
        // 遍历 fieldNameCountMap
        for (Map.Entry<String, Integer> entry : fieldNameCountMap.entrySet()) {
            // 获取 fieldName 和对应的出现次数
            String fieldName = entry.getKey();
            Integer count = entry.getValue();
            // 创建一个 LoanChannelFormRateStatisticVO 对象，并设置 fieldName 和 count
            LoanChannelFormRateStatisticVO vo = new LoanChannelFormRateStatisticVO();
            vo.setFieldName(fieldName);
            vo.setFieldTotal(count);
            // 将 LoanChannelFormRateStatisticVO 对象添加到结果列表中
            result.add(vo);
        }
        // 返回 LoanChannelFormRateStatisticVO 列表
        return result;
    }

}
